import { createLogger, format, transports } from "winston"
const isProd = process.env.NODE_ENV === "production"

const getLabel = function (callingModule: string) {
  if (!callingModule) {
    return ""
  }
  const parts = callingModule?.split(/\bserver\b/)
  return "server" + parts.pop()
}

export default function (callingModule: string) {
  return createLogger({
    // change level if in dev environment versus production
    level: isProd ? "http" : "debug",
    format: format.combine(format.label({ label: getLabel(callingModule) }), format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" })),
    transports: [
      new transports.Console({
        handleExceptions: true,
        format: format.combine(
          format.colorize(),
          format.printf((info) => `${info.timestamp} ${info.level} [${info.label}]: ${info.message}`)
        )
      })
    ]
  })
}
